package ink.xiaobaibai.common;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 手机验证码类
 * @author: 小白白
 * @create: 2021-05-16
 **/

public class SmsVerificationCode extends GeneralVerificationCode {

    private String phone;
    private static final String regex = "^(1[3-9]\\d{9}$)";

    public SmsVerificationCode(String phone) {
        this.phone = phone;
    }

    public boolean isValidPhone() {
        //验证手机验证码是否格式正确
        if (this.phone != null && this.phone.length() == 11) {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(this.phone);
            return m.matches();
        }
        return false;
    }

    public String getPhone() {
        return this.phone;
    }

    @Override
    protected String getRandomCode() {
        return RandomStringUtils.randomNumeric(codeLength);
    }
}
